{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "precise-conviction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         Iris-setosa\n",
       "1         Iris-setosa\n",
       "2         Iris-setosa\n",
       "3         Iris-setosa\n",
       "4         Iris-setosa\n",
       "            ...      \n",
       "145    Iris-virginica\n",
       "146    Iris-virginica\n",
       "147    Iris-virginica\n",
       "148    Iris-virginica\n",
       "149    Iris-virginica\n",
       "Name: 结果, Length: 150, dtype: object"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "iris= pd.read_csv('./iris.data',header=None,names=[\"属性1\", \"属性2\", \"属性3\", \"属性4\",\"结果\"])\n",
    "\n",
    "iris\n",
    "iris[\"结果\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tested-offset",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "satisfied-plymouth",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>属性1</th>\n",
       "      <th>属性2</th>\n",
       "      <th>属性3</th>\n",
       "      <th>属性4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     属性1  属性2  属性3  属性4\n",
       "0    5.1  3.5  1.4  0.2\n",
       "1    4.9  3.0  1.4  0.2\n",
       "2    4.7  3.2  1.3  0.2\n",
       "3    4.6  3.1  1.5  0.2\n",
       "4    5.0  3.6  1.4  0.2\n",
       "..   ...  ...  ...  ...\n",
       "145  6.7  3.0  5.2  2.3\n",
       "146  6.3  2.5  5.0  1.9\n",
       "147  6.5  3.0  5.2  2.0\n",
       "148  6.2  3.4  5.4  2.3\n",
       "149  5.9  3.0  5.1  1.8\n",
       "\n",
       "[150 rows x 4 columns]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "iris.loc[:,[\"属性1\", \"属性2\", \"属性3\", \"属性4\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "cleared-newport",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35     1\n",
       "51     2\n",
       "123    0\n",
       "122    0\n",
       "104    0\n",
       "75     2\n",
       "72     2\n",
       "116    0\n",
       "63     2\n",
       "131    0\n",
       "42     1\n",
       "14     1\n",
       "137    0\n",
       "128    0\n",
       "18     1\n",
       "7      1\n",
       "134    0\n",
       "142    0\n",
       "114    0\n",
       "81     2\n",
       "40     1\n",
       "80     2\n",
       "127    0\n",
       "103    0\n",
       "110    0\n",
       "26     1\n",
       "30     1\n",
       "39     1\n",
       "4      1\n",
       "82     2\n",
       "38     1\n",
       "139    0\n",
       "12     1\n",
       "44     1\n",
       "70     2\n",
       "105    0\n",
       "6      1\n",
       "52     2\n",
       "34     1\n",
       "32     1\n",
       "37     1\n",
       "129    0\n",
       "27     1\n",
       "16     1\n",
       "65     2\n",
       "Name: 结果, dtype: int64"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.切分数据集（10分）\n",
    "\n",
    "def ifs(x):\n",
    "    if x  =='Iris-setosa':\n",
    "        x=1\n",
    "    elif x == 'Iris-versicolor':\n",
    "        x=2\n",
    "    elif x == 'Iris-virginica':\n",
    "        x=0\n",
    "    return x\n",
    "iris['结果']=iris['结果'].map(ifs)\n",
    "Xtrain,Xtest,Ytrain,Ytest = train_test_split(iris.loc[:,[\"属性1\", \"属性2\", \"属性3\", \"属性4\"]],iris[ \"结果\"],test_size=0.3,random_state=260)\n",
    "Xtrain\n",
    "Ytrain\n",
    "Ytest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "increased-gospel",
   "metadata": {},
   "outputs": [],
   "source": [
    "#3.使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化（20分）\n",
    "\n",
    "std = StandardScaler().fit(Xtrain)\n",
    "Xtrain_ = std.transform(Xtrain)\n",
    "Xtest_ = std.transform(Xtest)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "cooperative-tracker",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=LogisticRegression(max_iter=1000),\n",
       "             param_grid={'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000],\n",
       "                         'penalty': ['l2']},\n",
       "             scoring='neg_log_loss')"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#4.在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合（20分）\n",
    "# 构建基分类器\n",
    "log_model = LogisticRegression(solver='lbfgs', max_iter=1000)\n",
    "\n",
    "penaltys = ['l2']\n",
    "Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]\n",
    "\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)\n",
    "\n",
    "grid = GridSearchCV(log_model,tuned_parameters,cv=5,scoring='neg_log_loss')\n",
    "\n",
    "grid.fit(Xtrain,Ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "living-aggregate",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>属性1</th>\n",
       "      <th>属性2</th>\n",
       "      <th>属性3</th>\n",
       "      <th>属性4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>5.6</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>5.7</td>\n",
       "      <td>2.8</td>\n",
       "      <td>4.1</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>5.5</td>\n",
       "      <td>2.3</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.3</td>\n",
       "      <td>4.4</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>5.4</td>\n",
       "      <td>3.4</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118</th>\n",
       "      <td>7.7</td>\n",
       "      <td>2.6</td>\n",
       "      <td>6.9</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>4.5</td>\n",
       "      <td>2.3</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>6.8</td>\n",
       "      <td>3.2</td>\n",
       "      <td>5.9</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5.4</td>\n",
       "      <td>3.7</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>105 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     属性1  属性2  属性3  属性4\n",
       "66   5.6  3.0  4.5  1.5\n",
       "2    4.7  3.2  1.3  0.2\n",
       "99   5.7  2.8  4.1  1.3\n",
       "53   5.5  2.3  4.0  1.3\n",
       "87   6.3  2.3  4.4  1.3\n",
       "..   ...  ...  ...  ...\n",
       "31   5.4  3.4  1.5  0.4\n",
       "118  7.7  2.6  6.9  2.3\n",
       "41   4.5  2.3  1.3  0.3\n",
       "143  6.8  3.2  5.9  2.3\n",
       "10   5.4  3.7  1.5  0.2\n",
       "\n",
       "[105 rows x 4 columns]"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xtrain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "complete-following",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.07720975270378072"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "amber-shopper",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 100, 'penalty': 'l2'}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最优参数\n",
    "grid.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "hazardous-chest",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#5.将最优的结果重新用来实例化模型，查看训练集和测试集下的分数（20分）\n",
    "#(注意多分类需要增加参数  average='micro')\n",
    "\n",
    "log_model = LogisticRegression(C=grid.best_params_['C'],penalty=grid.best_params_['penalty'],solver='lbfgs', max_iter=1000)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "acquired-cooking",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9777777777777777"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_model.fit(Xtrain,Ytrain)\n",
    "log_model.score(Xtest,Ytest)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "greatest-puzzle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9777777777777777"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#6.计算精准率（20分）\n",
    "from sklearn.metrics import precision_score, recall_score\n",
    "precision_score(Ytest, log_model.predict(Xtest),average='micro')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "played-drive",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
